package com.party.service.impl;



import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.party.domain.Blurb;

import com.party.domain.Img;
import com.party.domain.Product;
import com.party.mapper.ProductMapper;
import com.party.service.ProductService;

@Service
public class ProductServiceImpl implements	ProductService
{
	
	private final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
	
	@Autowired
	private ProductMapper productmapper;
	
	
	//添加商品
	public int addProduct(String type,String validTime,String city,String description,String title,String value)
	{
		Product pro=new Product();
		
		pro.setType(type);
		pro.setValidTime(validTime);
		pro.setCity(city);
		pro.setDescription(description);
		pro.setTitle(title);
		pro.setValue(value);
		pro.setAddTime(new java.sql.Date(new java.util.Date().getTime()));

		productmapper.inster(pro);
		return pro.getPid();
		
	}
	
	
	//添加商品详情
	public void addBlurb(int productId,String address,String numberPeople,String numberBed,String numberRoom,String supportingFacility,String blurbLabel,String supplement,String warmPrompt,String btype)
	{
		Blurb blurb=new Blurb();
		blurb.setProductId(productId);
		blurb.setAddress(address);
		blurb.setNumberBed(numberBed);
		blurb.setNumberPeople(numberPeople);
		blurb.setNumberRoom(numberRoom);
		blurb.setSupportingFacility(supportingFacility);
		blurb.setBlurbLabel(blurbLabel);
		blurb.setSupplement(supplement);
		blurb.setWarmPrompt(warmPrompt);
		blurb.setType(btype);
		productmapper.insterBlurb(blurb);
		
	}
	
	//添加图片
	public void addImage(int productid,String address)
	{
		Img img=new Img();
		img.setProductid(productid);
		img.setAddress(address);
		productmapper.insterImg(img);
	}
	
	//删除商品
	public void deleteProduct(int id) throws Exception
	{
		Product pro=productmapper.selectIdProduct(id);
		if (pro == null) {
            throw new Exception("无id为 " + id + " 的商品");
        }
		productmapper.delete(id);
	}
	
	//删除商品详情
	public void deleteBlurb(int id) throws Exception
	{
		Blurb blurb=productmapper.selectIdBlurb(id);
		if(blurb==null){
            throw new Exception("无id为 " + id + " 的商品详情");
        }
		productmapper.deleteBlurb(id);
	}
	
	//删除图片
	public void deleteImage(int id)throws Exception
	{
		List<Img> img=productmapper.selectIdImg(id);
		if(img==null){
            throw new Exception("无id为 " + id + " 的商品图片");
        }
		for(int i = 0;i<img.size();i++)
		{
			String path=img.get(i).getAddress();
			File file = new File(path);
			if (file.isFile() && file.exists()) {  
		        file.delete();
		        System.out.println("删除图片成功");
		    }
		}
		productmapper.deleteImg(id);
	}
	
	//分页获取商品
	public List<Product> findTypeProduct(String type,int page,int size) throws Exception
	{
		int start=size*(page-1);
		
		List<Product> pro=productmapper.selectTypeProduct(type, start, size);
		if(pro==null){
            throw new Exception("无类型为 " + type + " 的商品");
        }
		return pro;
	}
	
	public List<Product> FindBlurbTypeProduct(String type,int page,int size)throws Exception
	{
		int start=size*(page-1);
		List<Product> pro=productmapper.selectBlurbTypeProduct(type, start, size);
		if(pro==null){
            throw new Exception("无二级类型为 " + type + " 的商品");
        }
		return pro;
	}
	
	
	
	public Blurb getIdBlurb(int id) throws Exception
	{
		Blurb blurb=productmapper.selectIdBlurb(id);
		if(blurb==null){
            throw new Exception("无id为 " + id + " 的商品");
        }
		return blurb;
	}
	
	
	public List<Img> getIdImg(int id)throws Exception
	{
		List<Img> img=productmapper.selectIdImg(id);
		if(img==null){
            throw new Exception("无id为 " + id + " 的商品图片");
        }
		return img;
	}
	
	public Product getIdProduct(int id)throws Exception
	{
		Product pro=productmapper.selectIdProduct(id);
		if(pro==null){
            throw new Exception("无id为 " + id + " 的商品");
        }
		return pro;
	}
/*	public List<Commodity> getIdCommodity(int pid)throws Exception
	{
		List<Commodity> com=productmapper.findById(pid);
		if(com==null)
		{
			throw new Exception("无id为 " + pid + " 的商品");
		}
		return com;
	}*/
	
	
	
	public List<Product> findTypeceshi(String type) throws UnsupportedEncodingException
	{
		//type = java.net.URLDecoder.decode(type,"utf-8");
		
		List<Product> pro=productmapper.selectType(type);
		return pro;
	}
	
	public void updateIdProduct(Product pro)
	{
		pro.setAddTime(new java.sql.Date(new java.util.Date().getTime()));
		productmapper.updateProduct(pro);
		
	}
	
	
	public void updateIdBluct(Blurb blu)
	{
		productmapper.updateBlurb(blu);
	}
	
}
